<?php
include_once('../../app/connection.php');

if(isset($_POST['action']))
{
    $action = $_POST['action'];
    
    if($action == 'delete')
    {
        if(isset($_POST['mamh']))
        {
            $mamh = $_POST['mamh'];
            $malop = $_POST['malop'];
            
            $str = "SELECT * FROM lop WHERE MAMONHOC= '$mamh' AND MALOP = '$malop'";
            $check = mysql_query($str);
            if(mysql_num_rows($check) == 0)
                echo 'Lớp học '. $mamh.'.'.$malop.' không tồn tại'; 
            else {
                $sql = "UPDATE LOP SET TRANGTHAI = 0 WHERE MAMONHOC='$mamh' AND MALOP = '$malop'";
                $result = mysql_query($sql);       
                echo 'Đã xóa lớp ' . $mamh.'.'.$malop;
            }
        }
        
        if(isset($_POST['array']))
        {
            $array = $_POST['array'];
                        
            for($i=0; $i < count($array); $i++)
            {
                $sql = "UPDATE LOP SET TRANGTHAI = 0 WHERE mamh='".$array[$i]."'";
                $result = mysql_query($sql);
            }
            
        }
    }
    
    if($action == 'suggest')
    {
        if(isset($_POST['mamh']))
        {
            $mamh = $_POST['mamh'];
            $hocky =  $_POST['hocky'];
            $nam = $_POST['nam'];  
                
            $sql = "SELECT * FROM monhoc,lop WHERE monhoc.MAMONHOC = lop.MAMONHOC 
                AND lop.MAMONHOC LIKE '$mamh%' AND lop.HOCKY = '$hocky' AND NAM = '$nam' AND lop.TRANGTHAI = 1";
            $result = mysql_query($sql);
            
            if(mysql_num_rows($result) == 0)
                echo 'Môn học '. $mamh .' không tồn tại';
            else                
                while($row = mysql_fetch_assoc($result))
                {
                    echo $row['MAMONHOC'].'.'.$row['MALOP'].' -- '.$row['TENMONHOC'].
                    ' -- ca '.$row['CA'].' -- thu '.$row['THU'].'<br />';
                }
        }
    }
    if($action == 'load_list')
    {
        $hocky = $_POST['hocky'];
        $nam = $_POST['nam'];
    ?>
        <center><table>
            <tr style="background: #247CB9;">
                <td style="width: 100px; text-align: center; color: #fff;">Mã MH</td>
                <td style="width: 200px; text-align: center; color: #fff;">Tên MH</td>
                <td style="width: 100px; text-align: center; color: #fff;">Ca</td>
                <td style="width: 50px; text-align: center; color: #fff;">Thứ</td>
                <td style="width: 50px; text-align: center; color: #fff;">Phòng</td>
                <td style="width: 50px; text-align: center; color: #fff;">Chọn</td>
            </tr>
    <?php
            $sql = mysql_query("SELECT * FROM lop, monhoc WHERE lop.MAMONHOC = monhoc.MAMONHOC 
                AND lop.HOCKY = '$hocky' AND NAM = '$nam'");
            while($rows = mysql_fetch_assoc($sql))
            {
    ?>
                 <tr>
                 <td style="text-align: center;"><?php echo $rows['MAMONHOC']; ?></td>
                 <td><?php echo $rows['TENMONHOC']; ?></td>
                 <td style="text-align: center;"><?php echo $rows['CA']; ?></td>
                 <td style="text-align: center;"><?php echo $rows['THU']; ?></td>
                 <td style="text-align: center;"><?php echo $rows['PHONG']; ?></td>
                 <td style="text-align: center;"><input type="checkbox" name="chk_mh[]" id="chk_mh" value="<?php echo $rows['mamh']; ?>" /></td>
                 </tr>
        <?php } ?>
        </table></center>
    <?php
    }
}

?>